package com.jianggujin.zkgui;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

public class DubboDialog extends JDialog {
    private JTextArea name;
    private JTable paramTable;
    private DefaultTableModel paramTableModel;
    private JButton close;

    private DubboDialog(MainFrame mainFrame, String path) {
        super(mainFrame);
        initComponent(path);
        initDialog();
    }

    private void initComponent(String path) {
        this.name = new JTextArea();
        this.name.setEditable(false);
        this.name.setLineWrap(true);
        this.name.setRows(3);
        this.add(new JScrollPane(this.name), BorderLayout.NORTH);

        paramTableModel = new DefaultTableModel(new String[] { "键", "值" }, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        if (path != null) {
            try {
                path = URLDecoder.decode(path, "UTF-8");
            } catch (UnsupportedEncodingException e) {
            }
            int index = path.indexOf('?');
            if (index > -1) {
                this.name.setText(path.substring(0, index));
                String params = path.substring(index + 1);
                StringTokenizer tokenizer = new StringTokenizer(params, "&");
                while (tokenizer.hasMoreTokens()) {
                    String param = tokenizer.nextToken();
                    index = param.indexOf('=');
                    if (index > -1) {
                        paramTableModel.addRow(new String[] { param.substring(0, index), param.substring(index + 1) });
                    } else {
                        paramTableModel.addRow(new String[] { param, "" });
                    }
                }
            } else {
                this.name.setText(path);
            }
        }

        paramTable = new JTable(paramTableModel);

        this.add(new JScrollPane(paramTable));

        this.close = new JButton("关闭");
        this.close.addActionListener(e -> {
            this.dispose();
        });
        this.add(this.close, BorderLayout.SOUTH);
    }

    private void initDialog() {
        setTitle("Dubbo解码");
        setModal(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setSize(400, 300);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        // 设置窗体在显示器居中显示
        this.setBounds((width - getWidth()) / 2, (height - getHeight()) / 2, getWidth(), getHeight());
        this.setVisible(true);
    }

    public static void showDialog(MainFrame mainFrame, String path) {
        new DubboDialog(mainFrame, path);
    }
}
